package com.ruoyi.web.controller.system;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.IndexList;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.MobileLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.Banner;
import com.ruoyi.system.domain.LoginParams;
import com.ruoyi.system.domain.SysFileOne;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysFileOneService;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.ISysUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;


import com.ruoyi.common.core.controller.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 登录控制器
 *
 * @author SK
 * @since 2018/6/13
 */
@RestController
@RequestMapping("/mobile/user")
public class UserController extends BaseController{

    @Autowired
    private ISysFileOneService sysFileOneService;

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserService userService;


    @Autowired
    private TokenService tokenService;

    @Autowired
    private MobileLoginService loginService;

    @Autowired
    private ISysNoticeService noticeService;


    /**
     * 获取通知公告列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }
    /**
     * 注册用户
     *
     * @return -1 用户名或密码错误  -2 账号冻结  -3 账号锁定 1 成功  -4 验证码错误
     */
    @PostMapping("/registerUser")
    @ResponseBody
    public AjaxResult registerUser(HttpServletRequest request) {
        String phoneNo = request.getParameter("phoneNo");
        String validCode = request.getParameter("validCode");
        // 登录结果
        LoginParams loginParams = new LoginParams();
        loginParams.setPhoneNo(phoneNo);
        loginParams.setValidCode(validCode);
        return loginService.registerUser(loginParams);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getUserInfo")
    public AjaxResult getUserInfo(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser user = sysUserService.selectUserById(loginUser.getUser().getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        return ajax;
    }

    @PostMapping({"saveUserInfo"})
    @ResponseBody
    public AjaxResult saveUserInfo(SysUser user, HttpServletRequest request) {
        AjaxResult ajax = AjaxResult.success("个人信息修改成功！");
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        currentUser = sysUserService.selectUserById(currentUser.getUserId());
        if (StringUtils.isNotBlank(user.getNickName())) {
            currentUser.setNickName(user.getNickName());
        }
        if (StringUtils.isNotBlank(user.getEmail())) {
            currentUser.setEmail(user.getEmail());
        } else {
            currentUser.setEmail("");
        }
        if (StringUtils.isNotBlank(user.getPhonenumber())) {
            currentUser.setPhonenumber(user.getPhonenumber());
        } else {
            currentUser.setPhonenumber("");
        }
        if (StringUtils.isNotBlank(user.getSex())) {
            currentUser.setSex(user.getSex());
        }
        if(currentUser.getPhonenumber()!=null && !"".equals(currentUser.getPhonenumber())){
            currentUser.setPassword(SecurityUtils.encryptPassword(currentUser.getPhonenumber()));
            currentUser.setUserName(currentUser.getPhonenumber());
        }
        currentUser.setBloodType(user.getBloodType());
        currentUser.setIdCard(user.getIdCard());
        currentUser.setCompany(user.getCompany());
        currentUser.setNickName(user.getNickName());
        sysUserService.updateUser(currentUser);
        return ajax;
    }

    @RequestMapping("addNotice")
    public AjaxResult addNotice(HttpServletRequest request,@RequestParam String noticeTitle,@RequestParam String noticeContent,@RequestParam String noticeType) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysNotice notice = new SysNotice();
        notice.setNoticeContent(noticeContent);
        notice.setNoticeTitle(noticeTitle);
        notice.setNoticeType(noticeType);
        notice.setCreateBy(loginUser.getUsername());
        notice.setCreateTime(new java.util.Date());
        noticeService.insertNotice(notice);
        AjaxResult ajax = AjaxResult.success("建议已提交");
        return ajax;
    }

    @RequestMapping("bloodTypeList")
    @ResponseBody
    public AjaxResult userBloodTypelist(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        System.out.println(loginUser.getUser().getBloodType());
        SysUser user = new SysUser();
        user.setBloodType(loginUser.getUser().getBloodType());
        List<SysUser> list = userService.selectMobileUserList(user);
//        Map<String, List<SysUser>> listMap = list.stream()
//                .collect(Collectors.groupingBy(SysUser::getBloodType,HashMap::new,Collectors.toList()));
      IndexList index = new IndexList();
            index.setALLBloodType(list);
            List indexs= new ArrayList();
            indexs.add(index);
        AjaxResult ajax = AjaxResult.success("请求成功",indexs);
        return ajax;
    }

    @GetMapping("/filelist")
    public TableDataInfo filelist(SysFileOne sysFileOne)
    {
        sysFileOne.setStatus("0");
        sysFileOne.setFileType("0");

        List<Banner> imgs= new ArrayList<>();
        startPage();
        List<SysFileOne> list = sysFileOneService.selectSysFileOneList(sysFileOne);
        if(list !=null){
            list.forEach(file->{
                if(file.getRemark()!=null){
                    Banner img= new Banner();
                    img.setImage(file.getRemark());
                    imgs.add(img);
                }

            });
        }
        return getDataTable(imgs);
    }
}
